package com.bocloud.cmp.driver.zteadapter;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.PostDataFormat;
import com.bocloud.common.http.HttpClient;
import com.bocloud.common.model.Result;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class ZTEHttpRequest {
    private static Logger logger = LoggerFactory.getLogger(ZTEHttpRequest.class);
    HttpClient httpClient = null;
    private final String hostUrl = "/rest/osrms/report/host-resource-usage-statistics";
    private final String intanceUrl = "/rest/osrms/report/instance-resource-usage-statistics";
    private final String computeHyper = "/compute/v2/os-hypervisors";
    private final String instanceResourceUsage = "/rest/osrms/vmmeasure/instance-resource-usage-statistics";

    private String url;
    private String uuid;

    public ZTEHttpRequest(String url) {
        this.httpClient = new HttpClient();
        this.url = url;
    }

    public ZTEHttpRequest(String url, String uuid) {
        this.httpClient = new HttpClient();
        this.url = url;
        this.uuid = uuid;
    }

    public ZTEHttpRequest(String url, String uuid, PostDataFormat dataFormat) {
        this.httpClient = new HttpClient(dataFormat);
        this.url = url;
        this.uuid = uuid;
    }

    /**
     * 获取 主机性能数据
     * 
     * @return
     */
    public List<HostResourceModel> getHostResource() {
        Result result = httpClient.get(url.trim() + hostUrl);
        if (result.isSuccess()) {
            List<HostResourceModel> hostResource = JSONObject.parseArray(result.getMessage(), HostResourceModel.class);
            return hostResource;
        }
        return null;
    }

    /**
     * 获取虚拟机性能数据
     * 
     * @return
     */

    public List<InstanceResourceModel> getInstanceResource() {
        Result result = httpClient.get(url.trim() + intanceUrl);

        if (result.isSuccess()) {
            List<InstanceResourceModel> instanceResource = JSONObject.parseArray(result.getMessage(),
                    InstanceResourceModel.class);
            logger.info("Instance Resource {}", instanceResource.toString());
            return instanceResource;
        }
        return null;
    }

    /**
     * 获取宿主机数据
     * 
     * @param uuid
     * @return
     */
    public ZTEHostSystemModel getHost() {

        String hosturl = url.trim() + "/" + this.uuid + "/" + computeHyper;
        Result result = httpClient.get(hosturl);
        logger.info("result Resource {}", result.toString());
        if (result.isSuccess()) {
            ZTEHostSystemModel instanceResource = JSONObject.parseObject(result.getMessage(), ZTEHostSystemModel.class);
            return instanceResource;
        }
        return null;
    }

    /**
     * 虚拟机性能获取接口
     * 
     * @return
     */
    public List<ServerStaticsModel> getInstanceStatics(Map<String, Object> params) {
        String instanceurl = url.trim() + "/" + instanceResourceUsage;
        logger.info("getInstanceStatics url {}\n", instanceurl);
        Map<String, Object> header = Maps.newHashMap();
        header.put("Content-type", "application/json");
        logger.info("Request params {}\n", params);
        Result result = httpClient.post(header, params, instanceurl);
        logger.info("result Resource {}\n", result.toString());
        if (result.isSuccess()) {
            List<ServerStaticsModel> instanceStatics = Lists.newArrayList();
            instanceStatics = JSONArray.parseArray(result.getMessage(), ServerStaticsModel.class);
            return instanceStatics;
        }
        return null;
    }
}
